var USER_INF_API = "http://www.shanbay.com/api/user/info/";
var REVIEW_URL = "http://www.shanbay.com/review/";
var SHANBAY_LOGIN_URL = "http://www.shanbay.com/accounts/login/";
var SHANBAY_LOGOUT_URL = "http://www.shanbay.com/accounts/logout/";
var SHANBAY_HOME_PAGE = "http://www.shanbay.com/";

var NICKNAME;
var rt = window.external.mxGetRuntime();

$(function(){
	
    rt.onAppEvent = function(obj) {
        if(obj.type == "ACTION_SHOW") {
            checkLogin();
            //location.reload();
        }
    }

    checkLogin();
    var storage = rt.storage;
    var isopen = storage.getConfig("isopen");
    if(isopen == "true") {
        $("#isopen").prop("checked", true);
    }else{
        $("#isopen").prop("checked", false);
    }
});

function checkLogin(){
	$.get(USER_INF_API, function(data,status){
    	if(data["result"] == undefined || data["result"] == 0){
            if($("#loginpage").prop("src") != SHANBAY_LOGIN_URL){
                $("#loginpage").prop("src", SHANBAY_LOGIN_URL);
            }
    		$("#loginpage").show();
    		$("#logined").hide();
    	}else{
    		$("#logined").show();
            $("#loginpage").hide();
    		NICKNAME = data["nickname"];
    		$("#username").text(NICKNAME);
    	}
  });

	//handleJSONFromURL(USER_INF_API, function(data){
		//alert("abc");
		//alert(data);
	//});
}

function loginSuccess(){
	if(frames["loginpage"].location.href == SHANBAY_HOME_PAGE){
        frames["loginpage"].location.href = SHANBAY_LOGIN_URL;
    	checkLogin();
	}
}

function logout(){
    $.get(SHANBAY_LOGOUT_URL, function(){
        checkLogin();
        //location.reload();
    });
}

function changeopen(){
    rt.post("openStatus", $("#isopen").prop("checked"));
}